Load required libraries
library(readxl)
library(ggplot2)
library(plotly)
Read xls into a dataframe
crime_rates_df <- read.csv("C:/Masters/GitHub/Summer2023/DSC640-Data Presentation & Visualization/Week5&6/ex4-2/crimerates-by-state-2005.csv")
nrow(crime_rates_df)
## [1] 52
head(crime_rates_df,5)
## state murder forcible_rape robbery aggravated_assault burglary
## 1 United States 5.6 31.7 140.7 291.1 726.7
## 2 Alabama 8.2 34.3 141.4 247.8 953.8
## 3 Alaska 4.8 81.1 80.9 465.1 622.5
## 4 Arizona 7.5 33.8 144.4 327.4 948.4
## 5 Arkansas 6.7 42.9 91.1 386.8 1084.6
## larceny_theft motor_vehicle_theft population
## 1 2286.3 416.7 295753151
## 2 2650.0 288.3 4545049
## 3 2599.1 391.0 669488
## 4 2965.2 924.4 5974834
## 5 2711.2 262.1 2776221
R - SCATTER PLOT
fig <- plot_ly(data = crime_rates_df, x = ~population, y = ~murder) %>%
layout(xaxis = list(range = c(0,40000000),title = 'Population'),
title="R - Scatter Plot for Population vs Murder",
yaxis = list(title = 'Murder Rate'))
fig
R - BUBBLE CHART
fig <- plot_ly(crime_rates_df, x = ~population, y = ~murder, text = ~state,
type = 'scatter', mode = 'markers', size = ~murder, color = ~state, #colors = 'Paired',
marker = list( sizemode = 'diameter'))
fig <- fig %>% layout(xaxis = list(range = c(0,40000000),title = 'Population'),
title="R - Bubble Chart for Population vs Murder by State",
yaxis = list(title = 'Murder Rate'))
fig
R -DENSITY CHART
p <- ggplot(crime_rates_df, aes(burglary)) + geom_density() +
geom_histogram(aes(y=..density..),bins=30,color="blue",fill="lightblue")
facet_wrap(~ state)
## <ggproto object: Class FacetWrap, Facet, gg>
## compute_layout: function
## draw_back: function
## draw_front: function
## draw_labels: function
## draw_panels: function
## finish_data: function
## init_scales: function
## map_data: function
## params: list
## setup_data: function
## setup_params: function
## shrink: TRUE
## train_scales: function
## vars: function
## super: <ggproto object: Class FacetWrap, Facet, gg>
fig <- ggplotly(p)
fig <- fig %>% layout(title="R - Density Plot for Burglary",
yaxis = list(title = 'Murder Rate'))
fig